package com.aier.user.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.user.entity.Dynamic;
import com.aier.user.entity.DynamicComment;
import com.aier.user.entity.DynamicMessage;
import com.aier.user.entity.User;
import com.aier.user.service.IDynamicService;

@Scope("prototype")
@Component("userDynamicAction")
public class UserDynamicAction extends UserBaseAction{
    
    @Resource(name="dynamicService")
    private IDynamicService dynamicService;
    
    /**
     * 动态
     */
    private static final long serialVersionUID = 1L;
    
    private int s;  // 错误码
    
    private int page;  // 当前页数
    private int page_size;  // 每页的记录数
    private int page_count;  // 总页数
    private int count;  // 总记录数
    
    private Long uid;  // 用户的id
    private Integer sid;  // 学校id(学校动态)
    private Long cid;  // 班级id(班级动态)
    private String img;  // 图片名称
    private String content;  // 文字内容
    private Short type;  // 动态类型(1学校动态2班级动态3个人动态)
    
    private Long did;  //动态的id
    private Integer ptype;  // 类型：0赞1取消赞
    private Integer pcount;  // 赞的次数
    private Integer ccount;  // 评论的次数
    private Integer tcount;  // 转发的次数
    private Long dcid;  //动态评论的id
    private String rname;  // 回复的对象的昵称
    private Long dcrid;  // 回复id
    
    private Dynamic dynamic;  // 动态
    private List<Dynamic> dynamicList;  // 动态列表
    private List<DynamicComment> dcList;  //动态评论列表
    private List<User> userList;  //动态的赞的用户列表
    private List<DynamicMessage> dmList;  // 与我相关的动态消息
    
    
    private File[] image;  // 客户端上传的图片
    private String[] photoName;  // 上传图片的名称或路径
    
    @Override
    public String execute(){
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除图片
     * @return
     */
    public String deleteImage() {
        File fo = new File(SystemConfigStatic.fresh_upload_url + "origin/" +img);
        if (fo.exists()) {
            fo.delete();
        }
        File fs = new File(SystemConfigStatic.fresh_upload_url + "small/" +img);
        if (fs.exists()) {
            fs.delete();
        }
        File fb = new File(SystemConfigStatic.fresh_upload_url + "big/" +img);
        if (fb.exists()) {
            fb.delete();
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 发布动态
     * @return
     */
    public String publishDynamic() {
        s = dynamicService.insertDynamic(uid, sid, cid, content, img, type);
        return SUCCESS;
    }
    
    /**
     * 转发动态
     * @return
     */
    public String transmitDynamic() {
        s = dynamicService.insertOtherDynamic(uid, sid, cid, content, did, type);
        tcount = dynamicService.searchDynamic(did).getTcount();
        
        return SUCCESS;
    }
    
    /**
     * 评论动态
     * @return
     */
    public String commentDynamic() {
        s = dynamicService.insertDynamicComment(uid, did, content);
        ccount = dynamicService.searchDynamic(did).getCcount();
        
        return SUCCESS;
    }
    
    /**
     * 回复动态的评论
     * @return
     */
    public String replyDynamic() {
        s = dynamicService.insertDCR(uid, dcid,rname, content);
        return SUCCESS;
    }
    
    /**
     * 赞或取消赞动态
     * @return
     */
    public String praiseDynamic() {
        s = dynamicService.updateDynamicPraise(uid, did, ptype);
        pcount = dynamicService.searchDynamic(did).getPcount();
        
        return SUCCESS;
    }
    
    /**
     * 获取学校、班级、个人动态
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchDynamicList() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        
        Map<String, Object> map = dynamicService.searchAllDynamic(page, page_size,uid,cid,sid);
        
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer)map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            dynamicList = (List<Dynamic>) map.get(ICommonDAO.OBJ_LIST);
                
            if (null != dynamicList && dynamicList.size() > 0) {
                for (Dynamic dynamic : dynamicList) {
                    
                    // 转发的动态
                    if (dynamic.getOriginal() == 1) {
                        dynamic.setDynamic(dynamicService.searchDynamic(dynamic.getRelativeid()));
                    }
                    
                    // 本人是否已赞过（1赞过0没赞）
                    dynamic.setPflag((short)dynamicService.searchPraiseBySelf(uid, dynamic.getDid()));
                }
            }
                
        }
        s = 1;
        return SUCCESS;
    }
    
  
    
    /**
     * 获取某条动态的评论
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchCommentByDid() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = dynamicService.searchCommentByDid(page, page_size, did);
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            dcList = (List<DynamicComment>) map.get(ICommonDAO.OBJ_LIST);
            if (dcList != null && dcList.size() > 0) {
                for (DynamicComment dc : dcList) {
                    dc.setDcrList(dynamicService.searchCRByDcid(dc.getDcid()));
                }
            }
                
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除动态
     * @return
     */
    public String deleteDynamic() {
        s = dynamicService.deleteDynamic(did);
        return SUCCESS;
    }
    
    /**
     * 删除评论
     * @return
     */
    public String deleteComment() {
        s = dynamicService.deleteComment(dcid);
        return SUCCESS;
    }
    
    /**
     * 删除回复
     * @return
     */
    public String deleteReply() {
        s = dynamicService.deleteReply(dcrid);
        return SUCCESS;
    }
    
    /**
     * 获取某条动态的赞
     * @return
     */
    public String searchDynamicPraise() {
        userList = dynamicService.searchDynamicPraise(did);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 客户端发布动态
     * @return
     */
    public String publishDynamicApp() {
        String picName = "";
        if (null != image) {
            for (int i=0;i<image.length;i++) {
                File file = image[i];
                
                String fileName = ImageUtil.uploadImage(file, SystemConfigStatic.fresh_upload_url, photoName[i], 1);
                
                if (!"".equals(picName)) {
                    picName += ",";
                }
                picName += fileName;
            }
        }
        s = dynamicService.insertDynamic(uid, sid, cid, content, picName, type);
        return SUCCESS;
    }
    
    /**
     * 获取某条动态的详情
     * @return
     */
    public String searchDynamicDetail() {
        dynamic = dynamicService.searchDynamicDetail(uid, did);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 获取动态中与我相关的消息（好友评论了我的动态、回复了我的评论、赞了我的动态）
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchDynamicMessage() {
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String, Object> map = dynamicService.searchDynamicMessage(page, page_size, uid);
        if (null != map) {
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            page = (Integer) map.get(ICommonDAO.PAGE);
            
            dmList = (List<DynamicMessage>) map.get(ICommonDAO.OBJ_LIST);
        }
        s = 1;
        return SUCCESS;
    }
    
    
    public void setDynamicService(IDynamicService dynamicService) {
        this.dynamicService = dynamicService;
    }
    
    public int getS() {
        return s;
    }
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }
    
    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }
    
    public void setUid(Long uid) {
        this.uid = uid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }
    
    public void setContent(String content) {
        this.content = content;
    }
    
    public void setImg(String img) {
        this.img = img;
    }
    
    public void setType(Short type) {
        this.type = type;
    }

    public void setDid(Long did) {
        this.did = did;
    }

    public void setPtype(Integer ptype) {
        this.ptype = ptype;
    }
    
    public Integer getPcount() {
        return pcount;
    }
    
    public Integer getCcount() {
        return ccount;
    }

    public Integer getTcount() {
        return tcount;
    }
    
    public List<Dynamic> getDynamicList() {
        return dynamicList;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public void setPhotoName(String[] photoName) {
        this.photoName = photoName;
    }

    public void setDcid(Long dcid) {
        this.dcid = dcid;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public List<DynamicComment> getDcList() {
        return dcList;
    }

    public void setDcrid(Long dcrid) {
        this.dcrid = dcrid;
    }

    public List<User> getUserList() {
        return userList;
    }

    public Dynamic getDynamic() {
        return dynamic;
    }

    public List<DynamicMessage> getDmList() {
        return dmList;
    }



}
